/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package mapWriters;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import map.file.FileMap;

import figures.Figure;
import geometry.Rectangle;

public class ConfigMapWriter implements MapWriter {
	
	Rectangle area = null;
	List<LayerMapWriter> list = new LinkedList<LayerMapWriter>(); 
	
	
	public void addLayer(Class[] clases, long low, long high, boolean light){
		list.add(new LayerMapWriter(clases, low, high, light));
	}

	public void addFigure(Figure f) {
		for(LayerMapWriter l : list){
			if(area == null)
				area = new Rectangle(f.getBoundingBox());
			else
				area.unite(f.getBoundingBox());
			l.addFigure(f);
		}
	}

	public void write(DataOutputStream os) throws IOException {
		area.unload(os);
		os.writeShort(list.size());
		for(LayerMapWriter l : list){
			String name = FileNameSorter.sort();
			//Write index.
			os.writeLong(l.getLow());
			os.writeLong(l.getHight());
			FileMap.writeString(os, name);
			//Write the layer file.
			DataOutputStream o = new DataOutputStream(new FileOutputStream(name));
			l.write(o);
			o.close();
		}
	}

}
